package com.hxht.cmp.service.impl;

import com.hxht.cmp.common.Constants;
import com.hxht.cmp.entity.Case;
import com.hxht.cmp.entity.Plan;
import com.hxht.cmp.mapper.CaseMapper;
import com.hxht.cmp.mapper.CourtRoomMapper;
import com.hxht.cmp.pojo.CourtroomEx;
import com.hxht.cmp.service.CourtStatusService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 法庭状态
 */
@Service
public class CourtStatusServiceImpl implements CourtStatusService {
    @Resource
    private CourtRoomMapper courtRoomMapper;
    @Resource
    private CaseMapper caseMapper;

    @Override
    public List<Map<String, Object>> selectCourtStatusList(String courtUid) {
        try {
            //查看当前法院下的法庭是否有开庭信息
            List<Plan> plans = courtRoomMapper.selectStatusList(courtUid);
            LinkedList<Map<String, Object>> mapList = new LinkedList<>();
            for (CourtroomEx courtRoom : Constants.COURTROOM_LIST) {
                //获取当前法院下的法庭信息
                if (courtRoom.getCourtUid().equals(courtUid) && courtRoom.getActive() == 1) {
                    Map<String, Object> map = new LinkedHashMap<>();
                    map.put("courtUid", courtRoom.getCourtUid());
                    map.put("courtRoomName", courtRoom.getName());
                    map.put("courtroomUid", courtRoom.getUid());
                    map.put("status", "空闲");
                    map.put("deviceIp", courtRoom.getDeviceIp());
                    map.put("deviceType", courtRoom.getDeviceType());
                    map.put("cfg", courtRoom.getConfigs());
                    //匹配相应的法庭
                    for (Plan plan : plans) {
                        //案件信息对应的法庭
                        if (plan.getCourtRoomUid().equals(courtRoom.getUid())) {
                            Case case1 = new Case();
                            case1.setUid(plan.getCaseUid());
                            case1 = caseMapper.selectOne(case1);
                            map.put("caseUid", plan.getCaseUid());
                            map.put("planUid", plan.getUid());
                            if (!ObjectUtils.isEmpty(case1)) {
                                map.put("caseName", case1.getName());
                            }
                            if (plan.getStatus() == 0) {
                                map.put("status", "即将开庭");
                            } else if (plan.getStatus() == 1) {
                                map.put("status", "正在开庭");
                            } else if (plan.getStatus() == 2) {
                                map.put("status", "休庭");
                            }
                        }
                    }
                    mapList.addLast(map);
                }
            }
            mapList.sort((o1, o2) -> {
                System.out.println(o1.get("courtRoomName") + "\t" + o2.get("courtRoomName"));
                String status = o1.get("status").toString();
                String status2 = o2.get("status").toString();
                if ("空闲".equals(status) && "即将开庭".equals(status2)) {
                    return 1;
                } else if ("即将开庭".equals(status) && "空闲".equals(status2)) {
                    return 1;
                } else if ("空闲".equals(status2) && "空闲".equals(status)) {
                    return -1;
                } else {
                    return 0;
                }
            });
            return mapList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}